package com.signway.javaui.slice.component;

import com.signway.javaui.ResourceTable;
import com.signway.javaui.base.BaseAbilitySlice;
import com.signway.javaui.utils.SwLog;
import com.signway.javaui.utils.SwToast;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

public class ButtonComponent extends BaseAbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_component_button);

        initView();
    }

    /**
     * 校验组件正确性
     */
    private boolean checkView() {
        if (!(findComponentById(ResourceTable.Id_button) instanceof Button)) {
            SwLog.error("Id_button is not button");
            return false;
        }

        if (!(findComponentById(ResourceTable.Id_radio_container) instanceof RadioContainer)) {
            SwLog.error("Id_radio_container is not RadioContainer");
            return false;
        }

        if (!(findComponentById(ResourceTable.Id_check_box) instanceof Checkbox)) {
            SwLog.error("Id_check_box is not Checkbox");
            return false;
        }

        if (!(findComponentById(ResourceTable.Id_switch_button) instanceof Switch)) {
            SwLog.error("Id_switch_button is not Switch");
            return false;
        }

        if (!(findComponentById(ResourceTable.Id_toggle_button) instanceof ToggleButton)) {
            SwLog.error("Id_toggle_button is not ToggleButton");
            return false;
        }

        return true;
    }

    private void initView() {
        if (!checkView()) {
            terminate();
            SwLog.error("检验View失败");
            return;
        }

        Button button = (Button) findComponentById(ResourceTable.Id_button);
        button.setClickedListener(component -> SwToast.showToast(getTopAbilitySlice(), "Button 被点击了"));

        RadioContainer radioContainer = (RadioContainer) findComponentById(ResourceTable.Id_radio_container);
        radioContainer.setMarkChangedListener((radioContainer1, i) ->
                SwToast.showToast(getTopAbilitySlice(), "选择按钮编号:" + i + ", ID = " + radioContainer1.getMarkedButtonId()));

        Checkbox checkbox = (Checkbox) findComponentById(ResourceTable.Id_check_box);
        checkbox.setCheckedStateChangedListener((absButton, b) ->
                SwToast.showToast(getTopAbilitySlice(), "Check 状态改变：" + b));

        Switch switchBtn = (Switch) findComponentById(ResourceTable.Id_switch_button);
        switchBtn.setChecked(true);
        switchBtn.setCheckedStateChangedListener((absButton, b) ->
                SwToast.showToast(getTopAbilitySlice(), "Switch 状态改变：" + b));

        ToggleButton toggleButton = (ToggleButton) findComponentById(ResourceTable.Id_toggle_button);
        toggleButton.setChecked(true);
        toggleButton.setCheckedStateChangedListener((absButton, b) ->
                SwToast.showToast(getTopAbilitySlice(), "ToggleButton 状态改变：" + b));
    }
}
